<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (UV::readMQ($route->POST("act")) == "post")
{
	$sender_id = $user->getUserId();
	$recipient_id = UV::readInt($route->POST("whom"));
	$header = UV::readMQ(trim($route->POST("header")));
	if ($header == "")
		$header = "Без темы";
	$text = UV::readMQ(trim($route->POST("text")));
	if ($text == "")
		$text = "Без текста";
	$time = time();
	
	$success = $db->sendQuery("insert into `messages` values (NULL,'$sender_id','$recipient_id','$header','$text','$time',0,1,1)");
}

require_once COORDINATOR.DS."head.php";
?>
<div class="submenu_container">
	<a class="submenu_button" href='/coordinator/messages/received'>Полученные</a>
	<a class="submenu_button" href='/coordinator/messages/sended'>Отправленные</a>
	<a class="submenu_button submenu_button_active" href='/coordinator/messages/send'>Отправить</a>
</div>
<h3>Отправить сообщение</h3>
<?php
if (isset($success))
{
	if ($success)
		echo "<p style='color:green'>Ваше сообщение отправлено</p>";
	else
		echo "<p style='color:green'>Ошибка при отправке письма</p>";
}
?>
<form method='post'>
	<input type='hidden' name='act' value='post' />
	<table>
		<tr>
			<td>Кому:</td>
			<td>
				<select name='whom' style="width: 300px"><?php
					$user_list = $db->getTable("select `user_id`,`name`,`sname`,`login` from `users` where `user_id` != '".$user->getUserId()."' order by `name`");
					foreach($user_list as $whom_user)
					{
						echo "<option value='".$whom_user['user_id']."'";
						if ($route->getAdminId() == $whom_user['user_id'])
							echo " selected='selected'";
						echo ">".$whom_user['name']." ".$whom_user['sname']." (".$whom_user['login'].")</option>";
					}
				?></select>
			</td>
		</tr>
		<tr valign='top'>
			<td>Тема:</td>
			<td><input type='text' style="width:300px" name='header' /></td>
		</tr>
		<tr valign='top'>
			<td>Текст:</td>
			<td><textarea rows='5' style="width:300px" name='text'></textarea></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Отправить' /></td>
		</tr>
	</table>
</form>

<?php require_once COORDINATOR.DS."footer.php"; ?>
